package com.ce.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ce.core.AjaxResult;
import com.ce.entity.ShoppingList;
import com.ce.entity.Storage;
import com.ce.service.ShoppingListService;
import com.ce.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jyt
 * @since 2022-03-07
 */
@RestController
@RequestMapping("/shopping-list")
public class ShoppingListController {
    @Autowired
    ShoppingListService service;
    @Autowired
    StorageService storageService;
    /**
     * 查询采购金额统计 y坐标 ----图表
     */
    @GetMapping("/getShoppingListSumPrice/chart")
    public List<Object> selectPriceForChart() {
        List<ShoppingList> shoppingLists = service.getListForPrice();
        //定义List集合转化为纯数组
        List<Object> priceList = new ArrayList<>();
        for (int i = 0; i < shoppingLists.size(); i++) {
            //获取价格
            priceList.add(shoppingLists.get(i).getBuyPrice());
            System.out.println("salesLists.get(i).getSaleSum():" + shoppingLists.get(i));
        }
        return priceList;
    }

    /**
     * 查询采购商品名称统计 x坐标 ----图表
     */
    @GetMapping("/getShoppingListProductName/chart")
    public List<Object> selectNameForChart() {
        List<ShoppingList> shoppingLists = service.getListForPrice();
        //定义List集合转化为纯数组
        List<Object> nameList = new ArrayList<>();
        for (int i = 0; i < shoppingLists.size(); i++) {
            //获取年龄
            nameList.add(shoppingLists.get(i).getProductName());
            System.out.println("salesLists.get(i).getProductName():" + shoppingLists.get(i).getProductName());
        }
        return nameList;
    }

    /**
     * 查询采购清单信息
     */
    @PostMapping("/getShoppingList")
    public List<ShoppingList> testSelect(ShoppingList shoppingList) {
        List<ShoppingList> shoppingLists = service.getList(shoppingList);
        return shoppingLists;
    }

    /**
     * 查询采购金额(财务支出)
     */
    @PostMapping("/getShoppingListPrice")
    public AjaxResult selectPrice(ShoppingList shoppingList) {
        try {
            List<ShoppingList> shoppingLists = service.getListForPrice(shoppingList);
            return AjaxResult.success(shoppingLists);
        }catch (Exception e){
            return AjaxResult.error("请重置后再搜索");
        }


    }

    /**
     * 添加采购清单信息
     */
    @PostMapping("/addShoppingList")
    public Map<String, String> insertShoppingList(HttpServletRequest request) throws ParseException {
        ShoppingList shoppingList = new ShoppingList();
        shoppingList.setBuyerNo(request.getParameter("buyerNo"));
        shoppingList.setProductNo(request.getParameter("productNo"));
        shoppingList.setBuyDate(request.getParameter("buyDate"));
        shoppingList.setBuySum(Integer.parseInt(request.getParameter("buySum")));
        shoppingList.setBuyPrice(Double.parseDouble(request.getParameter("buyPrice")));
        Storage storage = new Storage();
        storage.setWarehouseNo(request.getParameter("warehouseNo"));
        storage.setProductNo(request.getParameter("productNo"));
        storage.setStorageSum(Integer.parseInt(request.getParameter("buySum")));
        System.out.println("warehouseNO:" + request.getParameter("warehouseNo"));
        // 更新库存 ---先查询是否有该商品库存信息，如果没有添加、有则更新
        if (ObjectUtils.isNotEmpty(storageService.selectByProductNoAndWarehouseNo(storage))) {
            storageService.updateStorageList(storage);
        } else {
            storageService.addStorageList(storage);
        }
        return service.addShoppingList(shoppingList);
    }
}
